package com.baymax.test.scene.demo

import android.app.Notification
import android.app.Service
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.os.Message
import android.os.Messenger
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.baymax.test.scene.*
import com.baymax.test.scene.internal.BackGround
import com.baymax.test.scene.provider.TestBinder
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.properties.Delegates


/**
 * @title:       标题
 * @project:     TestScene
 * @package:     com.baymax.test.scene.demo
 * @class:       NotificationScene
 * @description:
 * <p>
 * 类描述
 * </p>
 *
 * @version:   1.0
 * @createDate:   2022/11/4 10:40
 * @Copyright (C) 2022 YSTEN
 * @author:       xuyujian
 */
class NotificationScene : BaseScene() {

    var count: Job = Job()
    val manager by lazy { NotificationManagerCompat.from(requireSceneContext()) }
    val id = createNotificationId()
    override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): View {
        val context = this.requireActivity()
        val scene = this
         var messager :Messenger? = null
        val con = object : ServiceConnection {
            override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
                Log.d(TAG, "onServiceConnected:$service ")
                service?.linkToDeath({
                    Log.d(TAG, "client:: linkToDeath : dead")
                } , 0)
                messager = Messenger(service)
                Log.d(TAG, "onServiceConnected:${messager} ")
                val con1 = this
                launch {
                    messager?.send( Message.obtain(null, 1, 2, 3))
                    delay(300)
                    context.unbindService(con1)
                    Log.d(TAG, "unbindService: $con1 ")

                }
            }

            override fun onServiceDisconnected(name: ComponentName?) {
                Log.d(TAG, "onServiceDisconnected: ")
                context.unbindService(this)
                messager = null
            }

            override fun onBindingDied(name: ComponentName?) {
                super.onBindingDied(name)
                Log.d(TAG, "onBindingDied: $name")
            }
        }
        return p1.newScrollContainer {
            addButton("notifaction 1") {
                val serIntent = Intent(context, BackGround::class.java)
                ContextCompat.startForegroundService(context, serIntent)

                val r = context.bindService(serIntent, con, Context.BIND_AUTO_CREATE)
                Log.d(TAG, "bindService: $con $r")
//                scene.lifecycle.addObserver(object : DefaultLifecycleObserver {
//                    override fun onDestroy(owner: LifecycleOwner) {
//                        super.onDestroy(owner)
//                        Log.d(TAG, "onDestroy: unbindService $sercon")
//                        context.unbindService(sercon)
//                    }
//                })


            }
            addButton("test binder") {
                val serIntent = Intent(context, BackGround::class.java)
                ContextCompat.startForegroundService(context, serIntent)
                val r = context.bindService(serIntent, con, Context.BIND_AUTO_CREATE)
                Log.d(TAG, "bindService: $con $r")

            }

            addButton("mesager say"){
                launch {
                    messager?.send(Message.obtain(null, 1, 2, 3 ).apply {
                        data = bundleOf("hello" to scene.debugSceneHierarchy)
                    })
                }
            }

        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        manager.cancel(id)
        requireActivity().stopService(Intent(requireActivity(), BackGround::class.java))
    }

    private fun buildNotification(countDown: Int = 60): Notification =
        requireSceneContext().buildNotification {
            setAutoCancel(true)
                .setContentTitle("倒计时服务正在运行")
                .setContentText("${countDown}s后可以再次发送短信")
                .setVibrate(null)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(true)
        }

    companion object {
        val sercon = object : ServiceConnection {
            override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
                Log.d(TAG, "onServiceConnected:$service ")
                service?.linkToDeath({
                    Log.d(TAG, "client:: linkToDeath : dead")
                } , 0)
                (service as? TestBinder)?.foo()
            }

            override fun onServiceDisconnected(name: ComponentName?) {
                Log.d(TAG, "onServiceConnected: ")
            }

            override fun onBindingDied(name: ComponentName?) {
                super.onBindingDied(name)
                Log.d(TAG, "onBindingDied: $name")
            }
        }

    }

}